<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div>

            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">增下级</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script id="menuTemplate" type="text/html">

    <form  class="layui-form" action="" lay-filter="menuForm" style="margin-top:20px">
        <input  name="id" type="hidden"   /><br>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">菜单名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="title"  autocomplete="off" class="layui-input">
                    <input type="hidden" name="id" autocomplete="off" class="layui-input">
                    <input type="hidden" name="parentId" id="parentId"  autocomplete="off" class="layui-input">

                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">菜单图标</label>
                <div class="layui-input-inline">
                    <input type="text" name="icon"  autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">菜单地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="href"  autocomplete="off" class="layui-input"  >
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">排序号</label>
                <div class="layui-input-inline">
                    <input type="text" name="sequence"  autocomplete="off" class="layui-input"  >
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</script>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/lay-module/common/common.js" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['table', 'treetable','common','form'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        var form =layui.form;
        // 渲染表格


        var tableRender=function() {
        layer.load(2);

            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'parentId',
                elem: '#munu-table',
                url: '/menu/list',
                page: false,
                cols: [[
                    {type: 'id'},
                    {field: 'title', minWidth: 100, title: '菜单名称'},
                    {field: 'icon', minWidth: 100, title: '菜单图标'},
                    {field: 'href', title: '菜单地址'},
                    {field: 'sequence', width: 80, align: 'center', title: '排序号'},
                    {templet: '#auth-state', width: 180, align: 'center', title: '操作'}

                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };
        tableRender();

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $("body").on("click","#saveBtn",function (){
            $.post("/menu/save",form.val("menuForm"),function() {
                tableRender();
                layer.closeAll();
            });
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm("您确定要删除该菜单吗？",function(){
                    $.post("/menu/del?id="+data.id,function (){
                        tableRender();
                        layer.closeAll();
                    });
                });

            } else if (layEvent === 'edit') {
                $.get("/menu/"+data.id,function (m) {

                    layer.open({
                        type:1,
                        area:['500px','500px'],
                        title:"编辑菜单",
                        content:$("#menuTemplate").html()
                    });
                    form.val("menuForm",m);
                })

            }   else if (layEvent === "add"){
                layer.open({
                    type:1,
                    area:['450px','450px'],
                    title:"新增菜单",
                    content:$("#menuTemplate").html()
                });
                //把当前窗口的id传给弹出窗口的parentId中去
                $("#parentId").val(data.id);
            }
        });
    });
</script>
</body>
</html>